import { appTasks } from '@ohos/hvigor-ohos-plugin';


import fs from 'fs'
import path from 'path'
import { OhosPluginId } from '@ohos/hvigor-ohos-plugin';
import { getNode, hvigor } from '@ohos/hvigor'

const directory = '/Users/wxpmac/flutter_sdk/module_demo/module22/build/ohos/har/debug'
const rootNode: HvigorNode = getNode(__filename)
rootNode.afterNodeEvaluate(node => {
  const appContext = node.getContext(OhosPluginId.OHOS_APP_PLUGIN) as OhosAppContext
  const overrides = appContext.getOverrides() ?? {}
  const files = fs.readdirSync(directory);
  for (const file of files) {
    if (path.extname(file).toLowerCase() !== '.har') {
      continue
    }
    let depName = path.parse(file).name
    switch (depName) {
      case 'flutter':
        depName = '@ohos/flutter_ohos'
        break
      case 'flutter_module':
        depName = '@ohos/flutter_module'
        break
    }
    overrides[depName] = `file:${path.join(directory, file)}`
  }
  appContext.setOverrides(overrides)
})

export default {
  system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */
  plugins: []       /* Custom plugin to extend the functionality of Hvigor. */
}